package com.chaochis.controller;

import com.chaochis.config.page_config.api.ApiResult;
import com.chaochis.dto.CommentDto;
import com.chaochis.params.comment.AddCommentParam;
import com.chaochis.params.comment.UpdateCommentParam;
import com.chaochis.service.impl.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @author 刘超
 * @version 1.0
 * TODO
 * Created in 2024/11/24 19:33
 */
@RestController
@RequestMapping("/admin/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/add")
    private ApiResult<Integer> addComment(@RequestBody AddCommentParam param){
        var commentDto = new CommentDto();
        commentDto.setCommentContent(param.getCommentContent());
        commentDto.setUserId(param.getUserId());
        commentDto.setArticleId(param.getArticleId());
        commentDto.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        commentDto.setCommentId(UUID.randomUUID().toString());
        commentDto.setCommentScore(param.getCommentScore());
        return ApiResult.success(commentService.addComment(commentDto));
    }

    @PostMapping("/delete")
    private ApiResult<Integer> deleteComment(String commentId){
        var commentDto = new CommentDto();
        commentDto.setCommentId(commentId);
        return ApiResult.success(commentService.deleteComment(commentDto));
    }
    @PostMapping("/update")
    private ApiResult<Integer> updateComment(@RequestBody UpdateCommentParam updateCommentParam){
        var commentDto = new CommentDto();
        commentDto.setCommentId(updateCommentParam.getCommentId());
        commentDto.setCommentContent(updateCommentParam.getCommentContent());
        commentDto.setCommentScore(updateCommentParam.getCommentScore());
        return ApiResult.success(commentService.updateComment(commentDto));
    }
}
